Go direct to the page list if there are only one or two pages' worth of
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2
3 function wfSpecialAllpages( $par=NULL )
4 {
5 global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgLang;
6 $indexMaxperpage = 480;
7 $toplevelMaxperpage = 50;
8 $from = $wgRequest->getVal( 'from' );
9 $namespace = $wgRequest->getVal( 'namespace' );
10 if ( is_null($namespace) ) { $namespace = 0; }
11 $arr = $wgLang->getNamespaces();
12 $wgOut->setPagetitle ( $namespace > 0 ? wfMsg ( 'allpagesnamespace', $arr[$namespace] )
13 : wfMsg ( 'allarticles' ) );
14
15 if ( $par ) {
16 indexShowChunk( $par, $namespace );
17 } elseif ( $from ) {
18 indexShowChunk( $from, $namespace );
19 } else {
20 indexShowToplevel ( $namespace );
21 }
22 }
23
24 function namespaceForm ( $namespace = 0, $from = "" )
25 {
26 global $wgLang, $wgScript;
27
28 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
29
30 $namespaceselect = '<select name="namespace">';
31 $arr = $wgLang->getNamespaces();
32 for ( $i = 0; $i < 14; $i++ ) {
33 $namespacename = str_replace ( "_", " ", $arr[$i] );
34 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
35 $sel = ($i == $namespace) ? ' selected="selected"' : '';
36 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
37 }
38 $namespaceselect .= '</select>';
39
40 $frombox = '<input type="text" size="20" name="from" value="'
41 . htmlspecialchars ( $from ) . '"/>';
42 $submitbutton = '<input type="submit" value="' . wfMsg( 'go' ) . '" />';
43
44 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
45 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
46 $out .= wfMsg ( 'allpagesformtext', $frombox, $namespaceselect, $submitbutton );
47 $out .= '</form></div>';
48 return $out;
49 }
50
51 function indexShowToplevel ( $namespace = 0 )
52 {
53 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgLang, $wgRequest, $wgUser;
54 $sk = $wgUser->getSkin();
55 $fname = "indexShowToplevel";
56 $namespace = intval ($namespace);
57
58 # Cache
59 $vsp = $wgLang->getValidSpecialPages();
60 $log = new LogPage( $vsp["Allpages"] );
61 $log->mUpdateRecentChanges = false;
62
63 global $wgMiserMode;
64 if ( $wgMiserMode ) {
65 $log->showAsDisabledPage();
66 return;
67 }
68
69 $dbr =& wfGetDB( DB_SLAVE );
70 $cur = $dbr->tableName( 'cur' );
71 $fromwhere = "FROM $cur WHERE cur_namespace=$namespace";
72 $order_arr = array ( 'ORDER BY' => 'cur_title' );
73 $order_str = 'ORDER BY cur_title';
74 $out = "";
75 $where = array( 'cur_namespace' => $namespace );
76
77 $count = $dbr->selectField( 'cur', 'COUNT(*)', $where, $fname );
78 $sections = ceil( $count / $indexMaxperpage );
79
80 if ( $sections < 3 ) {
81 # If there are only two or less sections, don't even display them.
82 # Instead, display the first section directly.
83 indexShowChunk( '', $namespace );
84 return;
85 }
86
87 # We want to display $toplevelMaxperpage lines starting at $offset.
88 # NOTICE: $offset starts at 0
89 $offset = intval ( $wgRequest->getVal( 'offset' ) );
90 if ( $offset < 0 ) { $offset = 0; }
91 if ( $offset >= $sections ) { $offset = $sections - 1; }
92
93 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
94 # we're displaying only the very last section, we still need two DB queries to find the titles
95 $stopat = ( $offset + $toplevelMaxperpage < $sections )
96 ? $offset + $toplevelMaxperpage : $sections ;
97
98 # This array is going to hold the cur_titles in order.
99 $lines = array();
100
101 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
102 for ( $i = $offset; $i <= $stopat; $i++ ) {
103 if ( $i == $sections ) # if we're displaying the last section, we need to
104 $from = $count-1; # find the last cur_title in the DB
105 else if ( $i > $offset )
106 $from = $i * $indexMaxperpage - 1;
107 else
108 $from = $i * $indexMaxperpage;
109 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
110 $sql = "SELECT cur_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
111 $res = $dbr->query( $sql, $fname );
112 $s = $dbr->fetchObject( $res );
113 array_push ( $lines, $s->cur_title );
114 if ( $s = $dbr->fetchObject( $res ) ) {
115 array_push ( $lines, $s->cur_title );
116 }
117 $dbr->freeResult( $res );
118 }
119
120 # At this point, $lines should contain an even number of elements.
121 $out .= "<table style='background: inherit;'>";
122 while ( count ( $lines ) > 0 ) {
123 $inpoint = array_shift ( $lines );
124 $outpoint = array_shift ( $lines );
125 $out .= indexShowline ( $inpoint, $outpoint, $namespace );
126 }
127 $out .= "</table>";
128
129 $nsForm = namespaceForm ( $namespace );
130
131 # Is there more?
132 $morelinks = "";
133 if ( $offset > 0 ) {
134 $morelinks = $sk->makeKnownLink (
135 $wgLang->specialPage ( "Allpages" ),
136 wfMsg ( 'allpagesprev' ),
137 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
138 );
139 }
140 if ( $stopat < $sections-1 ) {
141 if ( $morelinks != "" ) { $morelinks .= " | "; }
142 $morelinks .= $sk->makeKnownLink (
143 $wgLang->specialPage ( "Allpages" ),
144 wfMsg ( 'allpagesnext' ),
145 'offset=' . ($offset + $toplevelMaxperpage)
146 );
147 }
148
149 if ( $morelinks != "" ) {
150 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
151 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
152 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
153 $out2 .= $morelinks . '</td></tr></table><hr />';
154 } else {
155 $out2 = $nsForm . '<hr />';
156 }
157
158 # Saving cache
159 $log->replaceContent( $out );
160
161 $wgOut->addHtml( $out2 . $out );
162 }
163
164 function indexShowline( $inpoint, $outpoint, $namespace = 0 )
165 {
166 global $wgOut, $wgLang, $wgUser;
167 $sk = $wgUser->getSkin();
168 $dbr =& wfGetDB( DB_SLAVE );
169
170 $inpointf = str_replace( "_", " ", $inpoint );
171 $outpointf = str_replace( "_", " ", $outpoint );
172 $queryparams = 'from=' . $dbr->strencode( $inpoint );
173 if ( $namespace > 0 ) { $queryparams .= '&namespace='.intval($namespace); }
174 $out = wfMsg(
175 'alphaindexline',
176 $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ), $inpointf, $queryparams ) . '</td><td>',
177 '</td><td align="left">' . $outpointf
178 );
179 return '<tr><td align="right">'.$out.'</td></tr>';
180 }
181
182 function indexShowChunk( $from, $namespace = 0 )
183 {
184 global $wgOut, $wgUser, $indexMaxperpage, $wgLang;
185 $sk = $wgUser->getSkin();
186 $maxPlusOne = $indexMaxperpage + 1;
187 $namespacee = intval($namespace);
188
189 $out = "";
190 $dbr =& wfGetDB( DB_SLAVE );
191 $cur = $dbr->tableName( 'cur' );
192 $sql = "SELECT cur_title FROM $cur WHERE cur_namespace=$namespacee AND cur_title >= '"
193 . $dbr->strencode( $from ) . "' ORDER BY cur_title LIMIT " . $maxPlusOne;
194 $res = $dbr->query( $sql, "indexShowChunk" );
195
196 ### FIXME: side link to previous
197
198 $n = 0;
199 $out = '<table style="background: inherit;" border="0" width="100%">';
200 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
201 $t = Title::makeTitle( $namespacee, $s->cur_title );
202 if( $t ) {
203 $link = $sk->makeKnownLinkObj( $t, $t->getText() );
204 } else {
205 $link = '[[' . htmlspecialchars( $s->cur_title ) . ']]';
206 }
207 if( $n % 3 == 0 ) {
208 $out .= '<tr>';
209 }
210 $out .= "<td>$link</td>";
211 $n++;
212 if( $n % 3 == 0 ) {
213 $out .= '</tr>';
214 }
215 }
216 if( ($n % 3) != 0 ) {
217 $out .= '</tr>';
218 }
219 $out .= '</table>';
220
221 $nsForm = namespaceForm ( $namespace, $from );
222 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
223 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
224 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
225 $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ),
226 wfMsg ( 'allpages' ) );
227 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
228 $out2 .= " | " . $sk->makeKnownLink(
229 $wgLang->specialPage( "Allpages" ),
230 wfMsg ( 'nextpage', $s->cur_title ),
231 "from=" . wfUrlEncode ( $s->cur_title ) );
232 }
233 $out2 .= "</td></tr></table><hr />";
234
235 $wgOut->addHtml( $out2 . $out );
236 }
237
238 ?>